﻿package com.merrycat.utils
{
	//haha
	//xixi
	public class MNumberUtils
	{
		public static function normalize(value : Number, minimum : Number, maximum : Number) : Number
		{
			return (value - minimum) / (maximum - minimum);
		}

		public static function interpolate(normValue : Number, minimum : Number, maximum : Number) : Number
		{
			return minimum + (maximum - minimum) * normValue;
		}

		public static function map(value : Number, min1 : Number, max1 : Number, min2 : Number, max2 : Number) : Number
		{
			return interpolate(normalize(value, min1, max1), min2, max2);
		}
		
		/**
		 * 获取区间数值
		 * @param value 		源数据
		 * @param min 			最小值
		 * @param max 			最大值
		 */
		public static function getNumWithRange(value:Number, min:Number, max:Number) : Number
		{
			var v : Number = Math.max(value, min);
			
			if(v != value)
			{
				return min;	
			}else
			{
				return Math.min(v, max);
			}
		}
	}
}